﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using DataClass;

namespace Hotel
{
    /// <summary>
    /// Summary description for WebServiceHotel
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebServiceHotel : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(EnableSession = true)]
        public bool DangNhap(string username, string password)
        {
            bool kq;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "DangNhap";
            string[] parameterNames = new string[] { "@username", "@password", "@thanhCong" };
            object[] parameterValues = new object[] { username, password };

            kq = bool.Parse(dataProvider.GetReturnString(parameterNames, parameterValues, "@thanhCong"));
            if (kq == true)
            {
                Session.Add("IsLogin", true);
                return true;
            }
            return false;
        }

        [WebMethod(EnableSession = true)]
        public void DangXuat()
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                Session.RemoveAll();
            }
        }

        [WebMethod]
        public int LaySoLuongPhongTrong(string MaLoaiPhong, string MaHangPhong, DateTime NgayBD, DateTime NgayKT)
        {
            object kq;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "LaySoLuongPhongTrong";
            string[] parameterNames = new string[] {"@maLoaiPhong", "@maHangPhong", "@ngayVao", "@ngayRa", "@maKhachSan", "@maThanhPho" };
            object[] parameterValues = new object[] { MaLoaiPhong, MaHangPhong, NgayBD, NgayKT, "1", "1"};

            kq = dataProvider.ExecuteScalar(parameterNames, parameterValues);
            con.CloseConnection();

            return (int)kq;
        }

        [WebMethod]
        public DataTable LayDSHangPhong()
        {
            DataTable kq;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "LayDSHangPhong";

            kq = dataProvider.GetDataTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(kq);
            
            con.CloseConnection();

            return ds.Tables[0];
        }

        [WebMethod]
        public DataTable LayDSLoaiPhong()
        {
            DataTable kq;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "LayDSLoaiPhong";

            kq = dataProvider.GetDataTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(kq);

            con.CloseConnection();

            return ds.Tables[0];
        }

        [WebMethod]
        public float LayGiaThanh(string maLoaiPhong, string maHangPhong)
        {
            float kq;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "LayGiaThanh";
            string[] parameterNames = new string[] { "@maLoaiPhong", "@maHangPhong", "@giaPhong"};
            object[] parameterValues = new object[] { maLoaiPhong, maHangPhong};

            string tg = dataProvider.GetReturnString(parameterNames, parameterValues, "@giaPhong");
            con.CloseConnection();
            kq = float.Parse(tg);

            return kq;
        }

        [WebMethod]
        public bool DatPhong(string maLoaiPhong, string maHangPhong, DateTime ngayBD, DateTime ngayKT, int soLuong, string maDoan)
        {
            bool thanhCong;
            ASSqlServer con = new ASSqlServer();
            con.OpenConnection();

            ASDataProvider dataProvider = new ASDataProvider();
            dataProvider.CommandType = CommandType.StoredProcedure;
            dataProvider.CommandText = "DatPhong";
            string[] parameterNames = new string[] { "@maLoaiPhong", "@maHangPhong", "@ngayVao", "@ngayRa", "@soLuong", "@maKhachSan", "@maThanhPho", "@maDoan", "@thanhCong"};
            object[] parameterValues = new object[] { maLoaiPhong, maHangPhong, ngayBD, ngayKT, soLuong, 1, 1, maDoan };

            string tg = dataProvider.GetReturnString(parameterNames, parameterValues, "@thanhCong");
            con.CloseConnection();
            thanhCong = bool.Parse(tg);

            return thanhCong;
        }
    }
}
